package com.celesea.auth.group.service;

import com.celesea.auth.group.dao.GroupDao;
import com.celesea.auth.group.entity.Group;
import com.celesea.auth.group.entity.GroupUser;
import com.celesea.auth.group.vo.GroupFilter;
import com.celesea.auth.group.vo.GroupVo;
import com.celesea.auth.role.entity.RoleCode;
import com.celesea.framework.entity.BaseEntity;
import com.celesea.framework.entity.BusEntity;
import com.celesea.framework.service.AbstractBaseService;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.Sort;
import com.celesea.framework.vo.DictionaryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author JackYuan
 * @create 2017-10-26 23:17
 */
@Service
public class GroupServiceImpl extends AbstractBaseService<Group> implements GroupService {

    public static final String SORT = "sort";
    public static final String COMPANY = "company";
    @Autowired
    private GroupDao groupDao;
    @Autowired
    private GroupUserService groupUserService;

    @Override
    public GroupVo selectById(String id) {
        Group entity = super.selectById(id);
        GroupVo role = new GroupVo(entity);
        List<GroupUser> users = groupUserService.findByGroup(id);
        if (CollectionKit.isNotEmpty(users)) {
            role.setUsers(users.stream().map(item ->
                    new DictionaryVo(item.getUserId(), item.getUserName())).collect(Collectors.toList()));
        }
        return role;
    }

    @Override
    public List<Group> selectGroups() {
        Example example = new Example(Group.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo(BusEntity.TALENT_ID, EnvUtil.getTenantId()).
                andEqualTo(BusEntity.DELETE_FLAG, Const.Bool.TRUE);
        if (!EnvUtil.hasAnyRoles(RoleCode.ADMIN_SYSTEM)) {
            criteria.andEqualTo(COMPANY, EnvUtil.getCompany());
        }
        example.orderBy(SORT).asc();
        return groupDao.selectByExample(example);
    }

    @Override
    public GroupVo insert(GroupVo entity) {
        Assert.isTrue(super.checkUnique(entity.toUnique()),"群组编码重复");
        Group group = super.insert(entity.toEntity());
        entity.setId(group.getId());
        groupUserService.insertByGroup(entity);
        return entity;
    }

    @Override
    public GroupVo update(GroupVo entity) {
        Assert.isTrue(super.checkUnique(entity.toUnique()),"群组编码重复");
        super.update(entity.toEntity());
        groupUserService.deleteByGroup(entity.getId());
        groupUserService.insertByGroup(entity);
        return entity;
    }

    @Override
    public List<Group> selectList() {
        GroupFilter filter = new GroupFilter();
        filter.setStatusFlag(Const.StatusFlag.ENABLE);
        Sort sort = new Sort();
        sort.setSortField(SORT);
        sort.setSortType(Sort.ASC);
        return super.selectList(filter, sort);
    }

    @Override
    public void disable(String id) {
        Assert.notNull(id, "请选择要停用的数据");
        Group group = new Group();
        group.setId(id);
        group.disable();
        int result = super.updateByExampleSelective(group);
        Assert.isTrue(result > 0, "停用失败！");
    }

    @Override
    public void enable(String id) {
        Assert.notNull(id, "请选择要启用的数据");
        Group group = new Group();
        group.setId(id);
        group.enable();
        int result = super.updateByExampleSelective(group);
        Assert.isTrue(result > 0, "启用失败！");
    }

    @Override
    public int doDelete(String id) {
        return super.doDelete(id);
    }

    private int updateStatus(String id, Group group) {
        Example example = new Example(group.getClass());
        Example.Criteria criteria1 = example.createCriteria();
        criteria1.andEqualTo(BaseEntity.PK, id).andEqualTo(BusEntity.TALENT_ID, EnvUtil.getTenantId());
        return groupDao.updateByExampleSelective(group, example);
    }
}
